<template>
	<view class="robot-message">
		<scroll-view scroll-y="true" style="max-height: 700rpx;" :scroll-top="scrollTop" scroll-with-animation>
			<view class="robot-message-item" v-for="item in msgList" :key="item.id">
				<imageForSize size="64" radius="50%" :src="item.src" />
				<view class="message-box" @click="goC2CChat(item)">
					<view class="name">
						{{item.name}}
					</view>
					<view class="time">
						{{item.time | formatDate}}
					</view>
					<view class="text">
						{{item.text}}
					</view>
				</view>
				<u-icon name="close" class="close-btn" @click="closeMsg(item)"></u-icon>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	let that = {}
	/* 把消息提示給提取出来 */
	export default {
		name: "robotMessage",
		data() {
			that = this
			return {
				scrollTop: 0,
				scrollTopVal: 10000000000,
			};
		},
		filters: {
			formatDate(val) {
				return val ? that.publicJS.timeZoneSwitching(val, 'HH:mm:ss DD-MM-YYYY') : ''
			}
		},
		watch: {
			msgList(val1, val2) {
				if (val2.length < val1.length) {
					console.log(this.scrollTopVal, 'scrollTopValscrollTopValscrollTopVal');
					this.$nextTick(() => {
						this.scrollTop = this.scrollTopVal++
					})
				}
				// console.log(val1, val2);
			},
		},
		computed: {
			msgList() {
				return this.vuex_online_message_list.filter(f => !f.hide)
			},
		},
		methods: {
			closeMsg(data) {
				this.$u.vuex('vuex_online_message_list', this.vuex_online_message_list.map(m => m.id === data.id ? {
					...m,
					hide: true
				} : m))
			},
			goC2CChat(data) {
				if (data.type == 'sys') {
					return
				}
				console.log('11123123123');
				this.$u.route('/pages/chat/chat', {
					id: data.from
				})
			}
			// scroll(e){
			// 	// this.scrollTop = e.detail.scrollTop + 80
			// 	console.log(e,'滾滾滾');
			// },
		}
	}
</script>

<style lang="scss" scoped>
	.robot-message {
		max-height: 700rpx;

		.robot-message-item {
			width: 0;

			&+.robot-message-item {
				margin-top: 20rpx;
			}

			position: relative;
			min-width: 360rpx;
			min-height: 124rpx;
			background: linear-gradient(270deg, rgba(35, 35, 39, 0.712) 0%, rgba(17, 17, 24, 0.72) 100%);
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			display: flex;
			padding: 8rpx;

			.message-box {
				color: #fff;
				font-size: 24rpx;
				margin-left: 12rpx;
				line-height: 34rpx;
				flex: auto;
				width: 0;

				.time {
					font-size: 16rpx;
					line-height: 16rpx;
					margin-top: 2rpx;
					margin-bottom: 4rpx;
				}

				.text {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.close-btn {
				color: #fff;
				position: absolute;
				right: 20rpx;
				top: 20rpx;
				font-size: 16rpx;
			}
		}
	}
</style>